//
//  ViewController.m
//  01-下拉放大图片
//
//  Created by 紫荆秋雪 on 16/3/21.
//  Copyright © 2016年 紫荆秋雪. All rights reserved.
//

#import "ViewController.h"
#import <Masonry.h>

#define RWImageViewHeight 300

@interface ViewController ()<UITableViewDataSource>

/** 应用imageView */
@property (nonatomic,weak) UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //1、创建一个
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img"]];
    
    //1.1、设置imageView显示模式
    imageView.contentMode = UIViewContentModeScaleAspectFill;

    //2、添加到父控件上
//    [self.tableView addSubview:imageView];
    [self.tableView insertSubview:imageView atIndex:0];
    
    CGFloat w = [UIScreen mainScreen].bounds.size.width;
    imageView.frame = CGRectMake(0, -RWImageViewHeight, w, RWImageViewHeight);
    
    self.imageView = imageView;
    
    //2.1设置tableView的 内边距
    self.tableView.contentInset = UIEdgeInsetsMake(RWImageViewHeight * 0.5, 0, 0, 0);
    
//    //3、设置“约束”
//    
//    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        //3.1设置宽度
//        make.width.equalTo(self.view.mas_width);
//        //3.2设置高度度
//        make.height.equalTo(@(RWImageViewHeight));
//        //3.3设置x
//        make.left.equalTo(self.view.mas_left);
//        //3.3设置y
//        make.top.equalTo(@((-RWImageViewHeight)));
//    }];
    
    
    
}

#pragma mark -<UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *ID = @"iamge";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] init];
    }
    
    //设置cell的数据
    NSString *dataStr = [NSString stringWithFormat:@"下拉放大图片- %zd",indexPath.row];
    
    cell.textLabel.text = dataStr;
    
    return cell;
}


#pragma mark - 监听滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    //1、获取下拉距离
    CGFloat downH = -(RWImageViewHeight * 0.5) - scrollView.contentOffset.y;
    
    //判断如果是上拉图片保持不变
    if (downH < 0) return;

    //2、获取当前imageView的frame
    CGRect frame = self.imageView.frame;
    
    frame.size.height = RWImageViewHeight + downH;
    
    self.imageView.frame = frame;
    
    //更新布局
//    [self.imageView mas_updateConstraints:^(MASConstraintMaker *make) {
//        //3.1设置宽度
//        make.width.equalTo(self.view.mas_width);
//        //3.2设置高度度
//        make.height.equalTo(@(RWImageViewHeight + downH));
//        //3.3设置x
//        make.left.equalTo(self.view.mas_left);
//        //3.3设置y
//        make.top.equalTo(@((-RWImageViewHeight)));
//        
//    }];
}

@end
